package com.estronger.shopmanage.adapter.tasking;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.bean.DeliverYPG;
import com.estronger.shopmanage.ui.send.SendDetailActivity;
import com.estronger.shopmanage.ui.send.pg.DeliverPGActivity;
import com.estronger.shopmanage.utils.ConvertUtils;
import com.estronger.shopmanage.utils.PermissionUtils;
import com.estronger.shopmanage.utils.TimeUtils;
import com.estronger.shopmanage.utils.Toast;
import com.estronger.shopmanage.view.AppDialog;

import java.util.ArrayList;

import cn.jesse.nativelogger.NLogger;


public class DeliverYPGListAdapter extends BaseAdapter implements OnClickListener {

    private DeliverPGActivity mContext;
    private ArrayList<DeliverYPG.Datas.DeliverYPGListData> mList;
    public Dialog dialog;

    public DeliverYPGListAdapter(DeliverPGActivity mContext, ArrayList<DeliverYPG.Datas.DeliverYPGListData> lt) {
        this.mContext = mContext;
        this.mList = lt;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (mList != null)
            return mList.size();
        else
            return 0;
    }

    @Override
    public DeliverYPG.Datas.DeliverYPGListData getItem(int position) {
        // TODO Auto-generated method stub
        if (mList == null || mList.size() <= 0)
            return null;
        else
            return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ListItemView ltv;

        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(
                    R.layout.adpter_deliver_yipai, null);
            ltv = new ListItemView();
            ltv.deliversname = (TextView) view.findViewById(R.id.deliversname);
            ltv.deliver_item_location = (TextView) view.findViewById(R.id.deliver_item_location);
            ltv.deliver_yytime = (TextView) view.findViewById(R.id.deliver_yytime);
            ltv.deliver_shy = (TextView) view.findViewById(R.id.deliver_shy);
            ltv.deliver_item_time = (TextView) view.findViewById(R.id.deliver_item_time);
            ltv.deliver_phone_call = (ImageView) view.findViewById(R.id.deliver_phone_call);
            ltv.deliver_send_sms = (ImageView) view.findViewById(R.id.deliver_send_sms);

            ltv.deliver_phone_call.setOnClickListener(this);
            ltv.deliver_phone_call.setTag(ltv);
            ltv.deliver_send_sms.setOnClickListener(this);
            ltv.deliver_send_sms.setTag(ltv);
            ltv.contentRl = view.findViewById(R.id.content_rl);
            view.setTag(ltv);
        } else {
            ltv = (ListItemView) view.getTag();
        }

        try {
            ltv.deliversname.setText(ConvertUtils.formatNullF(mList.get(position).buildname) + ConvertUtils.formatNull(mList.get(position).cstname));
            ltv.deliver_item_location.setText(ConvertUtils.formatNull(mList.get(position).address));
            ltv.deliver_yytime.setText(TimeUtils.cutsecond(mList.get(position).pddate));
            ltv.deliver_shy.setText(ConvertUtils.formatNull(mList.get(position).dlvmanname));
            ltv.deliver_item_time.setText(TimeUtils.cutsecond(mList.get(position).dlvassigndate));

            ltv._phone = mList.get(position).cstmobile;
            ltv.TitelName = ltv.deliversname.getText().toString();
            ltv.deliver_shy.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        int pos = (int) v.getTag(R.id.item_id_p);
                        String name = ConvertUtils.formatNull(mList.get(pos).dlvmanname);
                        if (!name.equals("")){
                            Toast.show(mContext, name, Toast.LENGTH_LONG);
                        }
                    }catch (Exception e){}

                }
            });
            ltv.deliver_shy.setTag(R.id.item_id_p, position);
            ltv.contentRl.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    String dlvid = mList.get(position).dlvid;
                    Intent intent = new Intent(mContext, SendDetailActivity.class);
                    intent.putExtra("dlvid", dlvid);
                    mContext.startActivity(intent);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        return view;
    }

    class ListItemView {
        String TitelName = "";
        public TextView deliversname, deliver_item_location, deliver_yytime, deliver_shy, deliver_item_time;
        public ImageView deliver_phone_call, deliver_send_sms;
        public String _phone = "";
        LinearLayout contentRl;
    }

    public String phone;

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.deliver_phone_call:// 点击了测量条目上的电话小图标
                ListItemView lv = (ListItemView) v.getTag();
                phone = lv._phone;
                if (mContext.isRightNumber(phone)) {
                    dialog = AppDialog.createDialog(mContext,
                            "[" + lv.TitelName + "]", phone, this, "");
                    dialog.show();
                }
                break;
            case R.id.deliver_send_sms:// 点击了测量条目上的发短信小图标
                lv = (ListItemView) v.getTag();
                phone = lv._phone;
                if (mContext.isRightNumber(phone)) {
                    Uri uri = Uri.parse("smsto:" + phone);
                    intent = new Intent(Intent.ACTION_SENDTO, uri);
                    mContext.startActivity(intent);
                }
                break;
            case R.id.ok:
                try {
                    if (PermissionUtils.isCallPhoneGranted(mContext)) {
                        intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
                                + phone));
                        mContext.startActivity(intent);
                        dialog.dismiss();
                    } else {
                        PermissionUtils.ToSetPermissions(mContext, R.string.PermissionsCallPhone);
                    }
                } catch (Exception e) {
                    NLogger.e("" + e.getMessage());
                }
                break;
        }
    }
}
